<?php
$isReloadable = false;
require_once("../../pers/inc.config.php");
require_once('../_inc/inc.session.php');
require_once('../_inc/funct.html.php');
require_once('../_inc/funct.docFile.php');

$docType = reqVar('type');
require '../_inc/inc.setDocParams.php';

switch (reqVar('azione')) {
   case 'save':
      $msgPag .= saveDoc($docType,$tab,$idName);
      break;
}
   
$sSql = "SELECT DISTINCT folder FROM $tab "
   ." WHERE $idName IN (0,".reqVar('id').") ORDER BY folder";
$objDB->query($sSql,'selYYY');
$aFolder = array('S'=>'-- no folder --');
while($acF = $objDB->fetch('selYYY')) {
   $fold = $acF['folder'];
   if ($fold=='') $fold='S';
   if (!key_exists($fold,$aFolder)) {
      if ($fold[0]=='S') {
         $foldX = 'Specific'.substr($fold,1);
      } else {
         $foldX = 'Global'.substr($fold,1);
      }
      $aFolder[$fold] = $foldX;
   }
}
   
$cmdL = "";

$cmdR = "";
$cmdR .= "<a class=\"cmd1\" href=\"javascript:submAct('save');\">save</a>";
$cmdR .= cmdTorna(); 

testaHtml($t1,$t2.' add',$msgPag,$menuCode);
?>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','...');?>
<?=getHtmlHidden('id');?>
<?=getHtmlHidden('type');?>
<?=getHtmlHidden('xsn',XSN);?>
<script>
   function submAct(act) {
      document.frm1.azione.value = act;
      document.frm1.submit();
   }
</script>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
<?
echo getHtmlRigaRif();
if ($headFunct!='') echo $headFunct(reqVar('id'));
?>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <? echo $cmdL; ?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <? echo $cmdR; ?>
      </td>
   </tr>
   <tr>
      <td class='tit1' colspan='20'>Document</td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>Folder</td>
      <td class='txt1' colspan='18'>
         <?
         echo getHtmlSelect('folder',$aFolder);?>
      </td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>Description</td>
      <td class='txt1' colspan='18'>
         <?
         echo getHtmlText('description',64 ,64);?>
      </td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>Note</td>
      <td class='txt1' colspan='18' valign='top'>
         <?
         echo getHtmlTextArea('note',80,2);?>
      </td>
   </tr>
   <tr>
      <td class='lab1' colspan='2'>File</td>
      <td class='txt1' colspan='18' valign='top'>
         <?
         echo getHtmlFile('filedoc',40);?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left" colspan='10'>
         <?//echo $cmdL; ?>
      </td>
      <td class="cmd1" align="right" colspan='10'>
         <? echo $cmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function saveDoc($docType,$tab,$idName) {
   global $objDB,$objSess;
   $msg = '';

   if (!isset($_FILES['filedoc']['tmp_name']) or $_FILES['filedoc']['tmp_name'] == "") {
      return "Select a file !<br>";
   }

   if ($msg != '') {
      return $msg;
   }
   $sFileName = $_FILES['filedoc']['name'];
   $sSql = "INSERT INTO $tab SET $idName = ".reqVar('id')
      .",folder = '".reqVarAS('folder')."' "
      .",description = '".reqVarAS('description')."' "
      .",note = '".reqVarAS('note')."' "
      .",uploading_time = '".date('Y-m-d H:i:s')."' "
      .",uploading_user = '".$objSess->idUtente."' "
      .",file_name = '".addslashes($sFileName)."' ";
	$objDB->exec($sSql,'insrec');
   $idDoc = $objDB->lastInsertId('insrec');
   $mess = '';
   $rc = putDocFile($docType, $objSess->idUtente, $idDoc, $sFileName, $_FILES['filedoc']['tmp_name'],$mess);
   if (!$rc) {
      return $mess;
   }
   
   backPage('doc added');
	exit();
}
